<?php
/*
Plugin Name: Achievements
Plugin URI: http://www.metabiscuits.com
Description: Provides Achievements similar to World of Warcraft or Xbox Live to BuddyPress.
Version: 0.1

Requires at least: WPMU 2.7.1, BuddyPress 1.0.2
Tested up to: WPMU 2.7.1, BuddyPress 1.0.2
License: tba
Author: Paul Gibbs
Author URI: http://www.twitter.com/pgibbs
Site Wide Only: true
*/
define( 'DPA_IS_INSTALLED', 1 );
define( 'DPA_VERSION', '0.1' );
define( 'DPA_DB_VERSION', '5' );

if ( !defined( 'DPA_SLUG' ) )
	define( 'DPA_SLUG', apply_filters( 'DPA_slug', 'achievements' ) );

define( 'DPA_REGISTERFUNCTION', 'dpa_register_' );

if ( file_exists( WP_PLUGIN_DIR . '/achievements/dp-achievements/languages/' . get_locale() . '.mo' ) )
	load_textdomain( 'dp-achievements', WP_PLUGIN_DIR . '/achievements/dp-achievements/languages/' . get_locale() . '.mo' );
	
require_once( 'dp-achievements/dp-achievements-classes.php' ); /* database access classes and functions */
require_once( 'dp-achievements/dp-achievements-templatetags.php' );
require_once( 'dp-achievements/dp-achievements-coreachievements.php' ); /* built-in achievements */
require_once( 'dp-achievements/dp-achievements-cssjs.php' );
require_once( 'dp-achievements/dp-achievements-admin.php' );


/**
 * dpa_install()
 *
 * Installs and/or upgrades the database tables
 */
function dpa_install() {
	global $wpdb, $bp;
	
	if ( !empty($wpdb->charset) )
		$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
	
	$sql[] = "CREATE TABLE {$bp->dp_achievements->table_name} (
					id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
					category varchar(100) NOT NULL,
					short_name varchar(100) NOT NULL,
					nice_name varchar(100) NOT NULL,
					description longtext NOT NULL,
					ranks int(100) NOT NULL,
					points int(100) NOT NULL,
					active int(1) NOT NULL,
					KEY short_name (short_name)
				 ) {$charset_collate};";
	$sql[] = "CREATE TABLE {$bp->dp_achievements->table_name_earnt} (
					id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
					achievement_id bigint(20) NOT NULL,
				  user_id bigint(20) NOT NULL,
					rank int(100) NOT NULL,
					achieved_at DATETIME NOT NULL
				 ) {$charset_collate};";
	$achievements = dpa_install_coreachievements();

	foreach ( (array) $achievements as $a ) {
		$sql[] = $wpdb->prepare("INSERT INTO {$bp->dp_achievements->table_name} ( category, short_name, nice_name, description, ranks, points, active ) VALUES ( %s, %s, %s, %s, %d, %d, %d );", $a['category'], $a['short_name'], $a['name'], $a['description'], 1, $a['points'], 1 );
	}

	require_once( ABSPATH . 'wp-admin/upgrade-functions.php' );
	dbDelta($sql);
	update_site_option( 'dpa_db_version', DPA_DB_VERSION );
}
	
/**
 * dpa_setup_globals()
 *
 * Sets up global variables.
 */
function dpa_setup_globals() {
	global $bp, $wpdb;
	
	$bp->dp_achievements->table_name = $wpdb->base_prefix . 'dpa_achievements';
	$bp->dp_achievements->table_name_earnt = $wpdb->base_prefix . 'dpa_achievements_earned';
	$bp->dp_achievements->image_base = WP_PLUGIN_URL . '/dp-achievements/images';
	$bp->dp_achievements->format_activity_function = 'dpa_format_activity';
	$bp->dp_achievements->format_notification_function = 'dpa_format_notifications';
	$bp->dp_achievements->slug = DPA_SLUG;

	$bp->version_numbers->dp_achievements = DPA_VERSION;
}
add_action( 'plugins_loaded', 'dpa_setup_globals', 5 );	
add_action( 'admin_menu', 'dpa_setup_globals', 1 );

/**
 * dpa_check_installed()
 *
 * Checks to see if the DB tables exist or if you are running an old version
 * of the component. If it matches, it will run the installation function.
 */
function dpa_check_installed() {	
	global $wpdb, $bp;

	if ( !is_site_admin() )
		return false;

	/* Need to check db tables exist, activate hook no-worky in mu-plugins folder. */
	if ( get_site_option('dpa_db_version') < DPA_DB_VERSION )
		dpa_install();
}
add_action( 'admin_menu', 'dpa_check_installed', 1 );

/**
 * dpa_setup_nav()
 *
 * This adds the sub level nav items to the navigation array.
 */
function dpa_setup_nav() {
	global $bp;

 	bp_core_add_nav_item( __( 'Achievements', 'dp-achievements' ), $bp->dp_achievements->slug );
	bp_core_add_nav_default( $bp->dp_achievements->slug, 'dpa_screen_index', 'summary' );

	$achievements_link = $bp->loggedin_user->domain . $bp->dp_achievements->slug . '/';
	bp_core_add_subnav_item( $bp->dp_achievements->slug, 'summary', __( 'Summary', 'dp-achievements' ), $achievements_link, 'dpa_screen' );
	$categories = dpa_get( 'DISTINCT category', array( array( 'type' => 'd', 'key' => 'active', 'value' => 1 ) ) );

	foreach ( $categories as $category_name ) {
		bp_core_add_subnav_item( $bp->dp_achievements->slug, strtolower($category_name), __( $category_name, 'dp-achievements' ), $achievements_link, 'dpa_screen' );		
	}

	if ( $bp->current_component == $bp->dp_achievements->slug ) {
		if ( bp_is_home() ) {
			$bp->bp_options_title = __( 'Categories', 'dp-achievements' );
		} else {
			$bp->bp_options_avatar = bp_core_get_avatar( $bp->displayed_user->id, 1 );
			$bp->bp_options_title = __( 'Categories', 'dp-achievements' );
		}
	}
}
add_action( 'wp', 'dpa_setup_nav', 2 );

// bp_current_action
function dpa_screen() {
	$page = attribute_escape( bp_current_action() );

	if ( $page == 'summary' ) {
		do_action( 'dpa_screen_summary' );
 		bp_core_load_template( 'achievements/summary' );
  } else {
		do_action( 'dpa_screen_category', $page );
		bp_core_load_template( 'achievements/category' );
	}
}

// Main logic for event detection and handling.
function dpa_main() {
	$achievements = dpa_get( 'short_name', array( array( 'type' => 'd', 'key' => 'active', 'value' => 1 ) ) );	

  // Strips out anything after underscores, for ranked achievements.
	foreach ( $achievements as $a ) { 
		$short_name = DPA_REGISTERFUNCTION . $a; 
		$i = strpos( $short_name, '_', strlen( DPA_REGISTERFUNCTION ) ); 
		if ( $i !== false )
			$short_name = substr( $short_name, 0, $i ); 

		$unique_achievements[] = $short_name; 
	}
	$unique_achievements = array_unique( $unique_achievements ); 

	foreach ( $unique_achievements as $achievement ) {
		if ( function_exists( $achievement ) ) {
			do_action( 'dpa_preregister_' . $achievement );
			call_user_func( $achievement );
			do_action( 'dpa_postregister_' . $achievement );
		}
	}

	do_action( 'dpa_registercustom', $unique_achievements );
}
add_action( 'wp', 'dpa_main', 2 );
?>